﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Helpers;
using Lead2012.Business;

namespace Lead2012.Repository
{
    public class EmailTemplateManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public void GetModel(EmailTemplateViewModel model, string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            int counts = 0;
            model.ItemList = GetAllEmailTemplate(keywords, sortBy, sortOrder, pageIndex, pageSize, ref counts);            

            // Calculate paging
            if (counts > 0)
            {
                model.TotalPage = (counts % pageSize == 0) ? counts / pageSize : (counts / pageSize + 1);
                model.TotalRecord = counts;
                model.PageIndex = pageIndex;
                model.PageSize = pageSize;
            }
        }

        public void GetModel(EmailTemplateViewModel model, int id)
        {
            model.EmailInfo = GetSingleBy(id);
           
        }

        #region #Common
        public List<EmailTemplate> GetAllEmailTemplate(string keywords, string sortBy, string sortOrder, int page, int take, ref int counts)
        {
            List<EmailTemplate> list = new List<EmailTemplate>();
            var items = new EmailTemplateRepository().GetAll();

            // Add to list
            foreach (var item in items)
            {
                EmailTemplate model = new EmailTemplate();
                model.TemplateId = item.TemplateId;
                model.FromEmail = item.FromEmail;
                model.ToEmail = item.ToEmail;
                model.CcEmail = item.CcEmail;
                model.Subject = item.Subject;
                model.BccEmail = item.BccEmail;
                model.IsBodyHtml = item.IsBodyHtml;
                list.Add(model);
            }

            if (!string.IsNullOrEmpty(keywords))
            {
                list = list.Where(c => c.FromEmail.ToLower().Contains(keywords.ToLower())).ToList();
            }

            counts = list.Count;

            
            // Add item to list
            switch (sortBy)
            {
                case "FromEmail":
                    {
                        if (sortOrder == "asc")
                            list = list.OrderBy(c => c.FromEmail).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.FromEmail).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                case "ToEmail":
                    {
                        if (sortOrder == "asc")
                            list = list.OrderBy(c => c.ToEmail).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.ToEmail).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                case "CcEmail":
                    {
                        if (sortOrder == "asc")
                            list = list.OrderBy(c => c.CcEmail).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.CcEmail).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                case "Subject":
                    {
                        if (sortOrder == "asc")
                            list = list.OrderBy(c => c.Subject).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.Subject).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                default:
                    {
                        if (sortOrder == "asc")
                            list = list.OrderBy(c => c.TemplateId).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.TemplateId).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
            }
            
            return list;
        }


        public EmailTemplate GetSingleBy(int id)
        {
            EmailTemplate model = new EmailTemplate();

            var item = (from c in entities.EmailTemplates where c.TemplateId == id select c).FirstOrDefault();
            if (item != null)
            {
                model.TemplateId = item.TemplateId;
                model.FromEmail = item.FromEmail;
                model.ToEmail= item.ToEmail;
                model.CcEmail = item.CcEmail;
                model.Subject = item.Subject;
                model.BccEmail = item.BccEmail;
                model.Body = item.Body;
                model.IsBodyHtml = item.IsBodyHtml;
            }

            return model;
        }
        #endregion
    }
}